package com.site.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.codehaus.plexus.util.StringUtils;

import com.site.util.extremetable.QueryPage;


/**
 * 积分商城用于分页标签，只能上一页和下一页
 * @author LinW
 */
public class PointPageTag extends TagSupport {

	private static final long serialVersionUID = 1L;

	private QueryPage queryPage;
	
	private String action;

	private String namespace;
	
	private Map<String,String> paramMap = new HashMap<String,String>();
	
	@Override
	public int doStartTag() throws JspException{
		queryPage = (QueryPage)pageContext.getRequest().getAttribute("queryPage");
		paramMap.clear();
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doEndTag() throws JspException {
		try {
			if(queryPage.getTargetPage() > 1) {
				pageContext.getOut().write("<a href=\"" + getPreviousPagePath() + "\" class=\"pre\"></a>");
			} else {
				pageContext.getOut().write("<a href=\"javascript:void(0);\" class=\"pre\"></a>");
			}
			if(queryPage.getPageCount() == 0) {
				pageContext.getOut().write("<span><b>" + queryPage.getTargetPage() + "</b>/1</span>");
			} else {
				pageContext.getOut().write("<span><b>" + queryPage.getTargetPage() + "</b>/" + queryPage.getPageCount() + "</span>");
			}
			if(queryPage.getTargetPage() < queryPage.getPageCount()) {
				pageContext.getOut().write("<a href=\"" + getNextPagePath() + "\" class=\"next\"></a>");
			} else {
				pageContext.getOut().write("<a href=\"javascript:void(0);\" class=\"next\"></a>");
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("翻页标签出错！");
		}
		return super.doEndTag();
	}
	
	private String getNextPagePath() {
		String path = getActionPath();
		StringBuffer path2 = new StringBuffer();
		path2.append("?queryPage.targetPage=" + (queryPage.getTargetPage() + 1));
		for(Map.Entry<String,String> param : paramMap.entrySet()) {
			if(!StringUtils.isEmpty(param.getKey()) && !StringUtils.isEmpty(param.getValue())) {
				try {
					path2.append("&" + param.getKey() + "=" + URLEncoder.encode(param.getValue(),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		path += path2.toString();
		return path;
	}

	private String getPreviousPagePath() {
		String path = getActionPath();
		StringBuffer path2 = new StringBuffer();
		path2.append("?queryPage.targetPage=" + (queryPage.getTargetPage() - 1));
		for(Map.Entry<String,String> param : paramMap.entrySet())
		{
			if(!StringUtils.isEmpty(param.getKey()) && !StringUtils.isEmpty(param.getValue()))
			{
				try {
					path2.append("&" + param.getKey() + "=" + URLEncoder.encode(param.getValue(),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		path += path2.toString();
		return path;
	}
	
	private String getActionPath() {
		String path = pageContext.getServletContext().getContextPath() + namespace + "/" + action + ".do";
		return path;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

	public Map<String, String> getParamMap() {
		return paramMap;
	}

	public void setParamMap(Map<String, String> paramMap) {
		this.paramMap = paramMap;
	}
}
